/*
 * @userName: 湫之恋
 * @Author: Seven
 * @Date: 2020-07-01 17:19:47
 * @LastEditors: Seven
 * @LastEditTime: 2020-07-01 22:51:02
 */
const mongoose = require('mongoose')

const estimate = mongoose.model('estimateModel')
module.exports.getEstimateAll = async function ({ pageSize, pageNumber }) {
    var itemNumber=[];
    var numbers = await estimate.find()
        .populate("rentId")
        .populate("hireId")
        .populate("carId")
        .populate("orderId")
        .countDocuments();
    var bashi=await estimate.find({estimateNumber:"巴适"}).countDocuments();
    var will=await estimate.find({estimateNumber:"将就"}).countDocuments();
    var veryPoor=await estimate.find({estimateNumber:"很差"}).countDocuments();
    itemNumber.push(bashi)
    itemNumber.push(will)
    itemNumber.push(veryPoor)
    var maxNumbers = Math.ceil(numbers / pageSize);

    if (pageNumber > maxNumbers && maxNumbers != 0) {
        pageNumber = maxNumbers;
    }

    var rows = await estimate.find()
        .populate("rentId")
        .populate("hireId")
        .populate("carId")
        .populate("orderId")
        .limit(pageSize - 0)
        .skip(pageSize * (pageNumber - 1))
        console.log(maxNumbers)
    return { rows, maxNumbers,itemNumber }

}
module.exports.getEstimateByEstimateNumber = async function ({ estimateNumber, pageSize, pageNumber}) {
    // console.log(estimateNumber)
    var numbers = await estimate.find({ estimateNumber: estimateNumber })
        .populate("rentId")
        .populate("hireId")
        .populate("carId")
        .populate("orderId")
        .countDocuments();
    var maxNumbers = Math.ceil(numbers / pageSize);

    if (pageNumber > maxNumbers && maxNumbers != 0) {
        pageNumber = maxNumbers;
    }
    var rows = await estimate.find({ estimateNumber: estimateNumber })
        .populate("rentId")
        .populate("hireId")
        .populate("carId")
        .populate("orderId")
        .limit(pageSize - 0)
        .skip(pageSize * (pageNumber - 1))
        console.log(maxNumbers)
    return { rows, maxNumbers }

}